<template>
	<view class="main-page" style="background: white;">
		<view class="header">
			<view class="row-container height-110 ml-30">
				<text class="font-333-32 font-weight-600">{{name}}</text>
			</view>
			<view class="divider"></view>
			<view class="single-box">
				<view class="row-container">
					<text class="sort font-999-28">序号</text>
					<text class="font-999-28 ml-30">权限名称</text>
				</view>
				<view class="switch-box">
					<text class="font-333-28 font-weight-600">开关</text>
				</view>
			</view>
			<view class="divider"></view>
		</view>
		<view class="column-container width-100p mt-220 mb-195">
			<view class="column-box width-100p" v-for="(item , index) in dataList" :key="index">
				<view class="single-box">
					<view class="row-container">
						<text class="sort font-333-28 font-weight-600">{{index + 1}}</text>
						<text class="font-333-28 font-weight-600 ml-30">{{item.name}}</text>
					</view>
					<view class="switch-box" @click="changeOpenAuthCode(item)">
						<image class="switch-ic"
						 v-if="openAuthCodeList.findIndex((auth) => auth == item.authCode) == -1"
						src="../../static/common/ic_switch_close.png"></image>
						<image class="switch-ic"  v-else
						src="../../static/common/ic_switch_open.png"></image>
					</view>
				</view>
				<view class="divider"></view>
			</view>
			
			
		</view>
		
		<view class="bottom">
			<view class="row-container-center width-100p height-120">
				<text class="save-btn" @click="handleSaveAuth">保存</text>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name:'' , 
				dataList:[],
				staffId:'',
				openAuthCodeList:[] , // 已开通的权限code list
			}
		},
		
		onLoad(options) {
			this.name = options.name
			this.staffId = options.staffId
			this.loadAuthority()
		},
		
		methods: {
			loadAuthority(){
				let that = this
				this.http({
					url:'/user/auth/findAuthList',
					method:'POST',
					data:{
						'staffId':this.staffId
					},
					success(res){
						if (res.code == 200){
							that.dataList = res.data
							
							that.openAuthCodeList = []
							res.data.forEach((item)=>{
								if (item.checkAuth == 1){
									that.openAuthCodeList.push(item.authCode)
								}
							})
						}
						uni.stopPullDownRefresh()
					},
					failed(e){
						uni.stopPullDownRefresh()
					}
				})
			},
			
			//修改权限开关
			changeOpenAuthCode(item){
				let index = this.openAuthCodeList.findIndex((authItem) => authItem == item.authCode)
				if (index == -1){
					this.openAuthCodeList.push(item.authCode)
				} else{
					this.openAuthCodeList.splice(index , 1)
				}
				
				console.log('codeList---' , this.openAuthCodeList)
			},
			
			//保存修改
			handleSaveAuth(){
				let that = this
				this.http({
					url:'/user/auth/saveOrUpdateStaffAuth',
					method:'POST',
					data:{
						'staffId':this.staffId,
						'authCodes' : this.openAuthCodeList.join(',')
					},
					success(res){
						if (res.code == 200){
							uni.navigateBack()
						} else{
							setTimeout(()=>{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							} , 500)
						}
					},
					failed(e){
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.header{
		display: flex;
		flex-direction: column;
		width: 100%;
		position: fixed;
		z-index: 50;
		background-color: white;
	}
	
	.divider{
		width: calc(100% - 30rpx);
		height: 1rpx;
		background: #EEEEEE;
		margin-left: 30rpx;
	}
	
	.single-box{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 110rpx;
		
		.sort{
			width: 60rpx;
			padding: 0rpx 30rpx;
		}
		
		.switch-box{
			width: 280rpx;
			height: 110rpx;
			background: #F8F8F8;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			
			.switch-ic{
				width: 75rpx;
				height: 45rpx;
			}
		}
	}
	
	.bottom{
		display: flex;
		flex-direction: column;
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 180rpx;
		background: white;
		z-index: 50;
		
		.save-btn{
			width: calc(100% - 60rpx);
			height: 88rpx;
			background: #F94C4B;
			border-radius: 10rpx;
			line-height: 88rpx;
			text-align: center;
			color: white;
			font-size: 32rpx;
			font-weight: 600;
		}
		
	}
	
</style>
